package ${package.ServiceImpl};

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinoits.microservicehlyy.dto.input.${entity}InputDTO;
import com.sinoits.microservicehlyy.dto.input.Select${entity}InputDTO;
import com.sinoits.orm.dao.${entity}DoMapper;
import com.sinoits.orm.pojo.${entity}DoExample;
import com.sinoits.orm.pojo.${entity}DoWithBlobs;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${table.comment!} 服务实现类
 * @author ${author}
 * @since ${date}
 */
@Service
public class ${entity}Service {

    @Resource
    private ${entity}DoMapper ${entity?uncap_first}DoMapper;

    /**
     * 新增 ${table.comment!}
     * @param inputDTO 输入dto
     * @return 是否新增成功
     */
    public Integer insert${entity}(${entity}InputDTO inputDTO) {

        ${entity}DoWithBlobs ${entity?uncap_first}DoWithBlobs = new ${entity}DoWithBlobs();
        BeanUtils.copyProperties(inputDTO, ${entity?uncap_first}DoWithBlobs);

        return ${entity?uncap_first}DoMapper.insertSelective(${entity?uncap_first}DoWithBlobs);
    }

    /**
      * 更新 ${table.comment!}
      * @param inputDTO 输入dto
      * @return 是否更新成功
      */
    public Integer update${entity}(${entity}InputDTO inputDTO) {

        ${entity}DoWithBlobs ${entity?uncap_first}DoWithBlobs = new ${entity}DoWithBlobs();
        BeanUtils.copyProperties(inputDTO, ${entity?uncap_first}DoWithBlobs);

        return ${entity?uncap_first}DoMapper.updateByPrimaryKeySelective(${entity?uncap_first}DoWithBlobs);
    }

    /**
      * 逻辑删除 ${table.comment!}
      * 0:未删除 1:已删除
      * @param id 主键
      * @return 是否删除成功
      */
    public Integer delete${entity}(Integer id) {

        ${entity}DoWithBlobs ${entity?uncap_first}DoWithBlobs = new ${entity}DoWithBlobs();

        ${entity?uncap_first}DoWithBlobs.setId(id);
        ${entity?uncap_first}DoWithBlobs.setIsDel(1);

        return ${entity?uncap_first}DoMapper.updateByPrimaryKeySelective(${entity?uncap_first}DoWithBlobs);
    }

    /**
      * 查询 ${table.comment!} 列表
      * @param selectInputDTO 查询dto
      * @return 列表信息
      */
    public PageInfo< ${entity}DoWithBlobs > select${entity}List(
            Select${entity}InputDTO selectInputDTO) {
        ${entity}DoExample example = new ${entity}DoExample();
        ${entity}DoExample.Criteria criteria = example.createCriteria();

        example.setOrderByClause("create_time desc");
        example.setDistinct(false);

        // 添加查询条件
        if (selectInputDTO.getId() != null) {
            criteria.andIdEqualTo(selectInputDTO.getId());
        }
//      if (StringUtils.isNotBlank(selectInputDTO.get   )) {
//          criteria.
//      }

        // 只筛选未删除的数据
        criteria.andIsDelEqualTo(0);

        PageHelper.startPage(selectInputDTO.getPageNum(), selectInputDTO.getPageSize());
        List< ${entity}DoWithBlobs > ${entity?uncap_first}Dos =
                ${entity?uncap_first}DoMapper.selectByExampleWithBLOBs(example);

        return new PageInfo<>(${entity?uncap_first}Dos);
    }
}
